Uec3.companyPermissionModule = Ext.extend(Ext.app.Module, {
    id: 'company_permission',
    objsCollection:new Ext.util.MixedCollection(true),
    getObjByKey:function(key){
		return this.objsCollection.get(key);
	},
    del:function(id){
		var grid=this.getObjByKey("grid");
		Ext.Msg.confirm('信息','确定要删除',function(btn){
		 	if(btn=='yes'){
		 		grid.loadMask.show();
			  	Ext.Ajax.request({
			  	url:Uec3.App.Constants.homeUrl+'/saleInvoice/base!delete.do',
			  	params:{'id':id},
        		success:function(response){
	        		var result=Ext.decode(response.responseText);
	        		if(result.success=='OK'){
	        			grid.getStore().reload();
	        		}else{
	        			Ext.Msg.alert('错误',result.success); 
	        		}
		        	grid.loadMask.hide();
        		},
        		failure:function(){
        			Ext.Msg.alert("信息",'网络问题');
        			grid.loadMask.hide();
        		}
			  	});
		    }
		});
	},
	view:function(id){
		var payInvoice_store=this.getObjByKey("store");
		var record=payInvoice_store.getById(id);
		var desktop =Uec3.App.getDesktop(); 
		desktop.launchWindowByParam("saleInvoice_operation",{id:id,winId:id}); 
	},
    getPanel:function(params) {
   	var module=this;
   	
   	var aAjax=new Ext.data.Connection();
   	Ext.util.Observable.capture(aAjax,function(e){
   		if(e=='beforerequest'){
			Ext.MessageBox.show({
                title : "提示",
                msg : "传输中,请稍后.....",
                buttons: false,
                width:200,
                closable:false,
                wait:true,
                modal:true,
                minWidth: this.minProgressWidth
              
            });
   		}else if(e=='requestcomplete'||e=='requestexception'){
   			Ext.MessageBox.hide();
   			if(e=='requestexception'){
   				Ext.Msg.alert("警告",'保存失败,网络延迟或断网,请稍后重写打开');
   			}
   		}
   	});
   	
	var tree = new Ext.tree.TreePanel({
        height: 400,
        width: 120,
        //border:true,
        root: {
            id: 'department',
            expanded:true, 
            text: '权限角色'
        },
        loader: new Ext.tree.TreeLoader({
            dataUrl:Uec3.App.Constants.appUrl+'/companyPermission/base!getRoleList.do',
            listeners:{
            	'loadexception':function(tl,node,response){
            		Ext.Msg.alert("错误","加载失败,请重新加载!");
            	}
            }
        }),
        listeners:{
        	'click':function(node,e){
        		if(node.leaf&&node.leaf==true){
        			grid.setTitle("<span style='color:red'>"+node.text+"权限--</span>"+"员工列表");
        			var store=module.getObjByKey("store");
        			store.baseParams={roleId:node.attributes.id};
        			store.load();
        		}
        	}
        }
        
    });

	var receivableArray=new Ext.util.MixedCollection(false);
	var record=Ext.data.Record.create( [ 
		{
			name : 'id',
			type : 'int',
			mapping : 'id'
		}, {
			name : 'name',
			type : 'string',
			mapping : 'name'
		}, {
			name : 'code',
			type : 'string',
			mapping : 'code'
		}, {
			name : 'departmentId',
			type : 'int',
			mapping : 'departmentId'
		} ,{
			name : 'departmentName',
			type : 'string',
			mapping : 'departmentName'
		},{
			name : 'gmtHire',
			type : 'date',
			dateFormat:'Y-m-d H:i:s',
			mapping : 'gmtHire'
		},{
			name : 'memo',
			type : 'string',
			mapping : 'memo'
		},{
			name : 'roleId',
			type : 'int',
			mapping : 'roleId'
		}]);
		var store=new Ext.data.Store( {
			proxy : new Ext.data.HttpProxy( {
				url : Uec3.App.Constants.homeUrl+'/companyPermission/base!getEmployeeList.do'
			}),
			pruneModifiedRecords : true,
			paramNames : {
				start : "start",
				limit : "limit",
				sort : "sort",
				dir : "dir"
			},
			remoteSort:true,
			reader : new Ext.data.JsonReader( {
				totalProperty : 'totalProperty',
				root : 'root'
			}, record),
			listeners:{
				'beforeload':function(){
					  grid.getSelectionModel().clearSelections();
				}
			}
		});
		module.objsCollection.add("store",store);

		
		// 初始化notice_grid表格对象
		var sm = new Ext.grid.CheckboxSelectionModel({
			listeners:{
			'beforerowselect':function(obj,rowIndex,keepExisting,record){
			
			},
			'rowselect':function (obj,rowIndex,recordF) {
				var roleId=store.baseParams.roleId;
				recordF.set("roleId",roleId);
			},
			'rowdeselect':function (obj,rowIndex,recordF) {
				//var roleId=store.baseParams.roleId;
				recordF.set("roleId",null);
			},
			'selectionchange':function(){
				//alert(1);
			}
		}
	});
		
		
	var columnModel = new Ext.grid.ColumnModel( [ 
		new Ext.grid.RowNumberer(), 
        sm,
		{
		header : '工号',
		width : 90,
		sortable : false,
		align:"right",
		dataIndex : 'code'
		},
		{
		header : '雇员姓名',
		width : 100,
		sortable : false,
		dataIndex : 'name'
		},
		{
		header : '所在部门',
		width : 100,
		sortable : false,
		dataIndex : 'departmentName'
		},
		{
		header : '入职时间',
		width : 100,
		sortable : false,
		align:"right",
		renderer:Ext.util.Format.dateRenderer("Y-m-d"),
		dataIndex : 'gmtHire'
		},
		{
		id:'companyPermission_autoExpandColumn',
		header : '备注',
		width : 300,
		sortable : false,
		dataIndex : 'memo'
		}]);
				
	var grid=new Ext.grid.GridPanel({
	    cm:columnModel,
	    width:780,
	    height:370,
		store : store,
		sm : sm,
		//iconCls : 'icon-grid',
		loadMask : {
			msg : '数据加载中...'
		},
		stripeRows : true,
		autoScroll : true,
		autoExpandColumn:'companyPermission_autoExpandColumn',
		title:"员工列表",
		
		bbar : new Ext.PagingToolbar({
			pageSize : 10,
			store : store,
			displayInfo : true,
			style : 'align:center',
			displayMsg : '第 {0} - {1} 条  共 {2} 条',
			emptyMsg : "没有记录",
			listeners:{

			}
		}),
		tbar:new Ext.Toolbar([
			'-',
			{
			text:'保存权限',
			iconCls : 'icon-save',
			handler:function(){
				var roleId=store.baseParams.roleId;
				if(!roleId){
					Ext.Msg.alert("提示","请先选中角色!");
					return;
				}
				var modified=store.getModifiedRecords();
				if(modified.length<1){
					Ext.Msg.alert("提示","请先修改角色权限,再保存!");
					return;
				}
				var roles=[];
				Ext.each(store.getRange(),function(record){
					roles.push(record.data);
				});
				var rolesJson=Ext.encode(encodeFilte(roles));
				aAjax.request({
				 	url:Uec3.App.Constants.homeUrl+'/companyPermission/base!save.do',
				 	params:{rolesJson:rolesJson,roleId:roleId},
				 	success:function(response,options){
				 		Ext.Msg.alert("信息","权限保存成功");
				 	}
				});
			
			}
			},'-']
		)
	});	
	module.objsCollection.add("grid",grid);
    Ext.util.Observable.capture(grid.getView(),function(e,view){
    	if(e=='refresh'){
    		Ext.each(store.getRange(),function(record){
    			if(record.data.roleId||record.data.roleId!=0){
    				grid.getSelectionModel().selectRow(store.indexOf(record),true);
    			}
    		});
    	}
    });

	var panel=new Ext.Panel({
	    layout: 'hbox',
	    items: [tree,grid]
	});
	return panel;
	},
	initEvent:function(){
		var module=this;
		var win=module.getObjByKey("win");
		win.on('show',function(){
			this.getObjByKey("store").load();
		},module,{single:true});
	},
   createWindow : function(params){
   	  var windowId=this.id;
   	  if(params&&params.winId){
   	  	windowId+="_"+params.winId;
   	  }
      var desktop = Uec3.App.getDesktop();   
      var win = desktop.getWindowByUecId(windowId);
      if(!win){ 
      	 var module=this;
         win = desktop.createWindow({
            uecId: windowId,
            width: 920,
            height: 400,
            resizable:false,
            title:'权限管理',
            iconCls: 'acc-icon-saleInvoice',
            shim: false,
            moduleObj:module,
            constrainHeader: true,
            maximizable: false,
            layout: "fit", 
            items:[module.getPanel(params)],
            listeners:{
            	/*'resize':function(win,width,height){
            		var top=this.items.get(0).items.first();
            		var grid=this.items.get(0).items.last();
            		var winMinWidth=900;
            		var winMinHeight=440;
            			alert(width);
            			alert(height);
            		if(width<winMinWidth||height<winMinHeight){
            			win.setWidth(winMinWidth);
            			win.setHeight(winMinHeight);
            			grid.setHeight(winMinHeight-35);
            		}else{
            			grid.setHeight(height-35);
            			grid.setWidth(width-120);
            			top.setWidth(120);
            			top.setHeight(height-20);
            			
            		}*/
            	}
            	/*'show':function(){
            		this.fireEvent('resize');
            	}*/
            
         });
         module.objsCollection.add("win",win);
         //module.initEvent();
      } 
      Ext.util.Observable.capture(win,function(e){
      	//alert(e);
      });
      win.show();
    /*  win.on('resize',function(win,width,height){
            		var top=win.items.get(0).items.first();
            		var grid=win.items.get(0).items.last();
            		var winMinWidth=900;
            		var winMinHeight=440;
            		if(width<winMinWidth){
            			win.setWidth(winMinWidth);
            		}
            		if(height<winMinHeight){
            			win.setHeight(winMinHeight);
            			grid.setHeight(winMinHeight-35);
            		}
            		if(width>winMinWidth){
            			grid.setWidth(width-120);
            			top.setWidth(120);
            			
            		}
            		if(height>winMinHeight){
            			grid.setHeight(height-35);
            			top.setHeight(height-20);
            			
            		}
            	});*/
   }
});
